<?php 
define('IN_ADAGPS', TRUE);
include_once('adagps.com/cfg.php');
include_once(CORE_PATH.'includes/summary_functions.php');
include_once(CORE_PATH.'database/commonDB.php'); 
if(!ConnectDB()) {
    die('Không kết nói được DATABASE');
}

$sql = 'select service_id,LPAD(time_auto_send_sms,2,0) as timesms,phone_auto_send_sms as phonenumber from cus_config c
join tbl_service s on s.active = 1 and s.id= c.service_id
where c.flg_auto_send_sms = 1 and HOUR(now())=time_auto_send_sms and datediff(s.expire_date,now())>0';
$array = $objDbUpdate->GetArray($sql); // lấy ra mãng dịch cần tổng hợp dữ liệu nhắn tin tự động
$count = count($array);
for($i=0;$i<$count;$i++){
    $service_id=$array[$i]['service_id'];
    $timesms=$array[$i]['timesms'];
    $phonenumber=$array[$i]['phonenumber'];
    CreateSMS_auto($service_id,$timesms,$phonenumber);
}

function CreateSMS_auto($service_id,$timesms,$phonenumber){ // $service_id để xác nhận dịch vụ nào cần gửi tin tự động; $time :integer xác nhận thời gian 
    global $objDbUpdate;
    
    $date = date('Y-m-d')." $timesms:00:00";
    $timebuffer = 24*60*60; // ngày 24 giờ * 60 Phút * 60 giây
   
   $sql = "  SELECT  sum(num*(price+area_extprice)) as total FROM cus_history_product_$service_id   
             WHERE flg=1  and  TIMESTAMPDIFF(second,date_update,'$date')<=$timebuffer and date_update < '$date';"; 
    $arr = array();
    $total = 0;
    try{
        $arr = $objDbUpdate->GetArray($sql);
        if(count($arr)>0)
        $total = $arr[0]['total'];       
    }
    catch(Exception $e){
        $arr = array();
    }
    
    $money_get = 0;
    $money_pay =0;
    $sqlpay = "select sum(money) as money,flg from cus_pay where 
     TIMESTAMPDIFF(second,date_created,'$date')<=$timebuffer and date_created < '$date'
      and service_id = '$service_id' group by flg ";
    $arrPay = $objDbUpdate->GetArray($sqlpay);
    for($i=0;$i<count($arrPay);$i++){
        if($arrPay[$i]['flg']==1){
           $money_get = $arrPay[$i]['money']; 
        }else{
           $money_pay = $arrPay[$i]['money']; 
        }
    }

    $money_revenue = $total - $money_pay + $money_get; 
    $datesend = date('d.m.Y');
    $timesend = "$timesms:00:00";
    $total = FormatMoney($total);
    $money_revenue = FormatMoney($money_revenue);
    $money_pay = FormatMoney($money_pay);
    $money_get = FormatMoney($money_get);
    
    $smscontent =  SMS_SYSTEM_MONEY_PRODUCT.$total. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_GET_OTHER.$money_get. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_PAY_OTHER.$money_pay.
     SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DOANHTHU.$money_revenue. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_CAL_FROM.$timesend. SMS_SYSTEM_DOWNLINE.SMS_SYSTEM_DATE.$datesend;  
    
    $arrPhone = explode(';',$phonenumber);
    $num_phone = count($arrPhone);
     
   $sql = "INSERT INTO tbl_sms(date_created, sms_type, sms_content, service_id, phone_number, num_phone, sms_timesend)
     values(now(),1,'$smscontent',$service_id,'$phonenumber',$num_phone,now())" ;
     $objDbUpdate->Execute($sql); 
}

CloseDB();

function FormatMoney($money){
    $len = strlen($money);
    $postInt = strpos($money,'.')===false?$len:strpos($money,'.');
    
    $arrAra=array();
    if($postInt<4) return $money;
    else{
        for($i=$postInt;$i>=3;$i=$i-3){
            $arrAra[] = substr($money,$i-3,$len);
            $money = substr($money,0,$i-3);
            $len=$i-3;
        }
        if(strlen($money)>0){
          $arrAra[]=  $money;
        }
        
    }
   // echo '<pre>'; print_r($arrAra); die();
    $count = count($arrAra);
    $result = '';
    for($i=$count-1;$i>0;$i--){
        $result.=$arrAra[$i].',';
    }
    $result .=$arrAra[0];
    return $result;
}
?>
